
function get_commit_list(){
local i=0
for id in `cat commit_list`
do
commit_id[$i]=$id
let i+=1
done

local len=${#commit_id[*]}
let len-=1
local j=0
for ind in `seq $len -1 0`
do
commit_ids[$j]=${commit_id[$ind]}
let j=j+1
done
echo ${commit_ids[*]}
}


function insert_inclusion_info() {
commit=$1
filename=$2
cd /home/hrx/linux_old1
version=$(git name-rev $commit|awk -F '/' '{print $2}'|awk -F '~' '{print $1}')
commit_long_id=$(git rev-parse $commit)
fix1=$(git log --pretty=format:"%s" $commit -1|grep -i "\ fix")
fix2=$(git log --pretty=format:"%b" $commit -1|grep -i fixes)
cd - > /dev/null

line1="mainline inclusion"
line2="from mainline-$version"
line3="commit $commit_long_id"

if [[ "$fix1" != "" ]] || [[ "$fix2" != "" ]];then
  line4="category: bugfix"
else
  line4="category: feature"
fi
line5="bugzilla: https://gitee.com/openeuler/kernel/issues/I8C0CX"
line6="G"
line7="Reference: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=${commit_long_id}"
line8="G"
line9="----------------------------------------------------------------------"
line10="G"

# start row
len=$(grep  "^Subject: \[" ${filename} | awk -F '] ' '{print $2}'|wc -L)
row=4
if [[ $len -ge 52 ]];then
  row=5
fi

for i in `seq 1 10`
do
  row=`expr $row + 1`
  line=$(eval echo '$line'${i})
  if [[ "$line" == "G" ]];then
    sed -i ${row}G $filename
  else
    sed -i "$row a$line" $filename
  fi
done
}

function main() {
commit_ids=($(get_commit_list))
local k=0
for filename in `ls *.patch`
do
  echo -e "\033[34m start processing $filename \033[0m"
  commit_id=$(echo ${commit_ids[k]})
  echo "it's commit id: $commit_id"
  insert_inclusion_info $commit_id $filename
  let k+=1
done
}

# need to modify commit_list variable
main
#insert_inclusion_info b95fcd2 "0043-perf-metricgroup-Fix-typo-in-comment.patch" 
